package org.yh.schedul;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class MySchedul {

	private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

	// 上次执行完毕后 再执行
	@Scheduled(fixedDelay = 1000)
	public void scheduleAtFixedDelay() throws InterruptedException {
		log.info("fixedDelay时间：" + sdf.format(new Date()));
		log.info("fixedDelay当前线程：" + Thread.currentThread().getName());
		Thread.sleep(2000);
	}

	// 上次执行开始后 再执行
	@Scheduled(fixedRate = 1000)
	// 开启异步任务，定时器默认是占一个线程
	@Async
	public void scheduleAtFixedRate() throws Exception {
		log.info("fixRate时间:" + sdf.format(new Date()));
		log.info("fixRate当前线程：" + Thread.currentThread().getName());
		Thread.sleep(2000);
	}

	// 每隔10s执行一次
	@Scheduled(cron = "0/10 * * * * *")
	public void test2() {
		log.info("cron时间：" + sdf.format(new Date()));
	}
}
